package main

import (
	"bloggo/bloggo"
	"bloggo/handlers"
	"context"
	"github.com/gin-gonic/gin"
	"github.com/thinkerou/favicon"
	"net/http"
	"os"
	"os/signal"
	"syscall"
)

func main() {
	//gin.SetMode(gin.ReleaseMode)
	router := gin.Default()
	router.Use(bloggo.Cors())
	router.Static(bloggo.GetFileLinkPrefix(), bloggo.Conf.Upload.Path)
	router.Use(favicon.New("./favicon.ico"))
	router.GET("/ping", func(c *gin.Context) { c.JSON(http.StatusOK, gin.H{"message": "pong"}) })
	// Use(bloggo.Authentication)之前的接口，都不用经过身份验证
	//以下的接口，都使用bloggo.Authentication中间件身份验证
	//router.Use(bloggo.Authentication)
	// 简单的路由组: api
	api := router.Group(bloggo.Conf.ProjectRootPath)
	{
		api.GET("/", bloggo.Wrapper(handlers.Index))
		api.POST("/report", bloggo.Wrapper(handlers.Report))
		api.POST("/register", bloggo.Wrapper(handlers.Register))
		api.POST("/login", bloggo.Wrapper(handlers.Login))
		api.GET("/articles", bloggo.Wrapper(handlers.Articles))
		api.GET("/articles/search", bloggo.Wrapper(handlers.ArticlesSearch))
		api.GET("/articles/:articleId", bloggo.Wrapper(handlers.ArticleById))
		api.POST("/articles/:articleId/like", bloggo.Wrapper(handlers.ArticleLikeById))
		api.GET("/articles/archives", bloggo.Wrapper(handlers.Archives))
		api.GET("/categories", bloggo.Wrapper(handlers.Categories))
		api.GET("/tags", bloggo.Wrapper(handlers.Tags))
		api.GET("/photos/albums", bloggo.Wrapper(handlers.Albums))
		api.GET("/albums/:albumId/photos", bloggo.Wrapper(handlers.PhotosByAlbumId))
		api.GET("/about", bloggo.Wrapper(handlers.About))
		api.GET("/messages", bloggo.Wrapper(handlers.Messages))
		api.POST("/messages", bloggo.Wrapper(handlers.PostMessages))
		api.GET("/links", bloggo.Wrapper(handlers.Links))
		api.POST("/comments", bloggo.Wrapper(handlers.PostComment))
		api.GET("/comments", bloggo.Wrapper(handlers.Comments))
		api.POST("/comments/:commentId/like", bloggo.Wrapper(handlers.CommentLike))
		api.GET("/comments/:commentId/replies", bloggo.Wrapper(handlers.CommentReplies))
		api.GET("/users/code", bloggo.Wrapper(handlers.SendCode))
		api.GET("/qqLogin", bloggo.Wrapper(handlers.QQLogin))
		api.GET("/oauth/login/qq", bloggo.Wrapper(handlers.QQLoginRedirectTarget))
	}
	// 简单的路由组: admin
	admin := router.Group(bloggo.Conf.ProjectRootPath + "/admin")
	{
		admin.GET("/", bloggo.Wrapper(handlers.Admin))
		admin.POST("/articles", bloggo.Wrapper(handlers.SaveOrUpdateArticles))
		admin.GET("/articles/:articleId", bloggo.Wrapper(handlers.AdminArticleById))
		admin.POST("/articles/images", bloggo.Wrapper(handlers.UploadImage))
		admin.GET("/articles", bloggo.Wrapper(handlers.AdminArticles))
		admin.PUT("/articles", bloggo.Wrapper(handlers.AdminLogicDeleteArticles))
		admin.DELETE("/articles", bloggo.Wrapper(handlers.AdminDeleteArticles))
		admin.GET("/categories/search", bloggo.Wrapper(handlers.CategorieSearch))
		admin.GET("/categories", bloggo.Wrapper(handlers.Categories))
		admin.POST("/categories", bloggo.Wrapper(handlers.SaveOrUpdateCategories))
		admin.DELETE("/categories", bloggo.Wrapper(handlers.DeleteCategories))
		admin.GET("/tags/search", bloggo.Wrapper(handlers.TagsSearch))
		admin.GET("/tags", bloggo.Wrapper(handlers.Tags))
		admin.POST("/tags", bloggo.Wrapper(handlers.SaveTag))
		admin.DELETE("/tags", bloggo.Wrapper(handlers.FetchDeleteTag))
		admin.GET("/comments", bloggo.Wrapper(handlers.AdminComments))
		admin.PUT("/comments/review", bloggo.Wrapper(handlers.ReviewComments))
		admin.DELETE("/comments", bloggo.Wrapper(handlers.DeleteComments))
		admin.GET("/messages", bloggo.Wrapper(handlers.AdminMessages))
		admin.PUT("/messages/review", bloggo.Wrapper(handlers.ReviewMessages))
		admin.DELETE("/messages", bloggo.Wrapper(handlers.DeleteMessages))
		admin.GET("/users", bloggo.Wrapper(handlers.AdminUsers))

		admin.GET("/test", bloggo.Wrapper(handlers.Test))
	}
	//router.Run("0.0.0.0:9090")
	// 监听端口
	srv := &http.Server{
		Addr:    "0.0.0.0:" + bloggo.Conf.Port,
		Handler: router,
	}

	//服务停止处理
	go func(srv *http.Server) {
		// 服务连接
		if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
			bloggo.Log("listen: %s\n", err.Error())
		}
	}(srv)
	quit := make(chan os.Signal, 1)
	signal.Notify(quit, syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT)
	flag := <-quit
	bloggo.Log("notify sigs ", flag.String())
	err := srv.Shutdown(context.Background())
	if err != nil {
		bloggo.Log("http shutdown err is", err.Error())
		return
	}
	bloggo.Log("http shutdown")
}
